package com.bawei.module_mine.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.module_mine.R
import com.bawei.module_mine.model.CarOrderModelItem
import com.bawei.module_mine.model.InspectionOrderModelItem
import com.bawei.module_mine.model.MineServiceModel
import com.bawei.module_mine.model.NewsModelItem
import com.bawei.module_mine.model.RepairOrderModelItem
import com.bawei.module_mine.model.VisitOrderModelItem

class AuditNewsAdapter : RecyclerView.Adapter<AuditNewsAdapter.NewsViewHolder>() {
    private val newsAuditList= mutableListOf<NewsModelItem>()
    fun setNewsData(data:List<NewsModelItem>){
        newsAuditList.clear()
        newsAuditList.addAll(data)
        notifyDataSetChanged()
    }
    override fun getItemViewType(position: Int): Int {
        return position
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
        return NewsViewHolder(View.inflate(parent.context, R.layout.item_car_audit, null))
    }

    override fun getItemCount(): Int {
        return newsAuditList.size
    }

    override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
        val newsOrder = newsAuditList[position]
        val colorRes = when (newsOrder.status) {
            "待审核" -> com.bawei.lib_common.R.color.color_308fff
            "已拒绝"-> com.bawei.lib_common.R.color.color_yellow_dark
            "未阅读" -> com.bawei.lib_common.R.color.color_12aa00
            else -> android.R.color.black // 默认颜色
        }
        when (newsOrder.status) {
            "待审核" -> holder.newsState.setBackgroundResource(R.drawable.shape_item_repair_one)
            "已拒绝" -> holder.newsState.setBackgroundResource(R.drawable.shape_item_repair_two)
            "未阅读" -> holder.newsState.setBackgroundResource(R.drawable.shape_item_repair_there)
            else -> android.R.color.black // 默认颜色
        }
        when(newsOrder.status){
            "已拒绝" -> {
                holder.newsYes.visibility = View.GONE
                holder.newsNo.visibility = View.GONE
            }
            "未阅读" -> {
                holder.newsYes.visibility = View.GONE
                holder.newsNo.visibility = View.GONE
            }
        }
        holder.newsState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
        holder.newsState.text = "${newsOrder.status}"
        holder.newsPlane.text = "物业管理处"
        holder.newsTime.text = "发布时间: ${newsOrder.publishTime}"
        holder.newsAudit.text = "园区公告审核"
        holder.newsName.text = "标题: ${newsOrder.title}"
        holder.newsPurpose.text = "内容: ${newsOrder.content}"
    }
    class NewsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val newsState: TextView = view.findViewById(R.id.item_audit_state)
        val newsAudit : TextView = view.findViewById(R.id.item_audit)
        val newsPlane: TextView = view.findViewById(R.id.item_audit_plane)
        val newsName: TextView = view.findViewById(R.id.item_audit_name)
        val newsPurpose: TextView = view.findViewById(R.id.item_audit_purpose)
        val newsTime: TextView = view.findViewById(R.id.item_audit_time)
        val newsYes: Button = view.findViewById(R.id.item_audit_yes)
        val newsNo: Button = view.findViewById(R.id.item_audit_no)
    }
    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
        recyclerView.addItemDecoration(GridSpaceItemDecoration(
            horizontalSpacing = spacing,  // 水平间距（左右）
            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.setItemViewCacheSize(20)
    }
}